package com.sc.byahi.user.client.components.maps;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.ui.Widget;

public class InfoWindow extends MVCObject<InfoWindow> {
	// singleton
	private static InfoWindow iw;

	public static InfoWindow getInfoWindow(InfoWindowOptions options) {
		closeExistingInfoWindow();
		if (iw == null) {
			if(options == null){
				options = InfoWindowOptions.getInstance();
			}
			iw = InfoWindow.getInstance(options);
		}
		return iw;
	}
	
	public static void closeExistingInfoWindow() {
		if (iw != null){
			iw.close();
			iw = null;
		}
	}
	
	protected InfoWindow() {
	}

	private static final InfoWindow getInstance(InfoWindowOptions options) {
		return createJso(options).cast();
	}

	private static final native JavaScriptObject createJso(InfoWindowOptions options) /*-{
		return new $wnd.google.maps.InfoWindow(options);
	}-*/;

	/**
	 * Closes this InfoWindow by removing it from the DOM structure.
	 */
	public final native void close() /*-{
		this.close();
	}-*/;

	public final native String getContent_String() /*-{
		return this.getContent();
	}-*/;

	public final native Element getContent_Node() /*-{
		return this.getContent();
	}-*/;

	public final native LatLng getPosition() /*-{
		return this.getPosition();
	}-*/;

	public final native int getZindex() /*-{
		return this.getZIndex();
	}-*/;

	/**
	 * Opens this InfoWindow on the given map. Optionally, an InfoWindow can be associated with an anchor. In the core API, the only anchor is the
	 * Marker class. However, an anchor can be any MVCObject that exposes the position property and optionally anchorPoint for calculating the
	 * pixelOffset (see InfoWindowOptions). The anchorPoint is the offset from the anchor's position to the tip of the InfoWindow.
	 * 
	 * @param mapWidget
	 */
	public final void open(MapWidget mapWidget) {
		open(mapWidget.getJso(), null);
	}

	/**
	 * Opens this InfoWindow on the given map. Optionally, an InfoWindow can be associated with an anchor. In the core API, the only anchor is the
	 * Marker class. However, an anchor can be any MVCObject that exposes the position property and optionally anchorPoint for calculating the
	 * pixelOffset (see InfoWindowOptions). The anchorPoint is the offset from the anchor's position to the tip of the InfoWindow.
	 * 
	 * @param mapWidget
	 * @param anchor
	 */
	public final void open(MapWidget mapWidget, MVCObject<?> anchor) {
		open(mapWidget.getJso(), anchor);
	}

	private final native void open(MapImpl impl) /*-{
		this.open(impl);
	}-*/;

	private final native void open(MapImpl impl, MVCObject<?> anchor) /*-{
		this.open(impl, anchor);
	}-*/;

	private final native void open(MapImpl impl, JavaScriptObject anchor) /*-{
		this.open(impl, anchor);
	}-*/;

	public final void setContent(Widget widget) {
		setContent(widget.getElement());
	}

	public final native void setContent(String content) /*-{
		this.setContent(content);
	}-*/;

	public final native void setContent(Element element) /*-{
		this.setContent(element);
	}-*/;

	public final native void setOptions(InfoWindowOptions options) /*-{
		this.setOptions(options);
	}-*/;

	public final native void setPosition(LatLng position) /*-{
		this.setPosition(position);
	}-*/;

	public final native void setZindex(int index) /*-{
		this.setZIndex(index);
	}-*/;

}
